<?

//Data loading
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';
/*
function filterSkada(){
    $filter = array('335','345','44','45','035','045','240','250');
    $arrFilter = array();
    foreach($filter as $f){
        $arrFilter[] = "skada LIKE '".$f."%'";
    }
    return "(".implode(' OR ',$arrFilter).")";
}

list($day,$month,$year) = explode("/",GetParam("agingDate",""));
$current_date = "$year-$month-$day";


$where1 = "";
$where2 = "";

        if (GetParam("office","")){
             $where1 .= " AND h.ktrasl = '".GetParam("office","")."' ";
             $where2 .= " AND p.ktrasl = '".GetParam("office","")."' ";
        }
        if (GetParam("curr","")){
             $where1 .= " AND h.curr = '".GetParam("curr","")."' ";
             $where2 .= " AND p.curr = '".GetParam("curr","")."' ";
        }
        if (GetParam("debturCode","")){
             $where1 .=" AND h.debtur  = '".GetParam("debturCode","")."' ";
             $where2 .=" AND p.debtur  = '".GetParam("debturCode","")."' ";
        }

$SQL = "SELECT
            SUM(IF(h.srt=1, IF(h.curr='IDR',-h.rupiah,-h.jumlah) , IF(h.curr='IDR',h.rupiah,h.jumlah) ))+
                CAST(
                    IFNULL( (SELECT SUM(IF(p.curr='IDR',p.rupiah,p.jumlah)) FROM ais_piutang AS p WHERE p.normut = 999 AND p.tglbkt<='2009-01-31' AND p.debtur = h.debtur $where2) ,0)
                AS DECIMAL(30,2)) total_org,
            SUM(IF(h.srt=1,-h.rupiah,h.rupiah))+
                CAST(
                    IFNULL( (SELECT SUM(p.rupiah) FROM ais_piutang AS p WHERE p.normut = 999 AND p.tglbkt<='2009-01-31' AND p.debtur = h.debtur $where2) ,0)
                AS DECIMAL(30,2)) total_rph FROM ais_hutang AS h
            WHERE h.tglbkt<='$current_date' AND ".filterSkada().$where1;
$SQL .= "GROUP BY h.debtur HAVING total_rph IS NOT NULL ORDER BY h.debtur,h.curr";
*/
include 'ais_report_soa_receivable_functions.php';
$pdf=new PDFReport();
$pdf->setData(getTotal(GetParam('debturCode',''),true));
$pdf->Exec();
$pdf->Output();


class PDFReport extends FPDF {
    var $widths = array(20,65,40,40,10);
    var $data = NULL;
    var $curr = 'IDR';

    function PDFReport(){
        parent::FPDF('P','mm','A4');
        $this->AddFont('arialn','','arialn.php');
        $this->SetFont('arialn','',9);
        $this->curr = GetParam("curr","");
    }

    function Header(){
        global $perush,$DBConnection;
        $this->Cell(190,5,$perush,0,1,'C');
        $this->Cell(190,5,'RECAPITULATION STATEMENT OF ACCOUNT RECEIVABLE',0,1,'C');
        $arrMonth = array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
        $this->Cell(190,5,'AS AT : '.strtoupper($arrMonth[GetParam("searchMonth","")]).' '.GetParam('searchYear',''),0,1,'C');
        $this->Ln();

        $this->Cell($this->widths[0],8,'CUST. NO',1,0,'C');
        $this->Cell($this->widths[1],8,'PRINCIPAL',1,0,'C');
        $this->Cell($this->widths[2],8,'AMOUNT (ORG)',1,0,'C');
        $this->Cell($this->widths[3],8,'AMOUNT (RPH)',1,0,'C');
        $this->Cell($this->widths[4],8,'PAGE',1,1,'C');
    }

    function setData($data){
        $this->data = $data;
    }

    function Exec(){
        $totalORG = 0;
        $totalRPH = 0;

        $this->AddPage();
        if($this->data){
          foreach($this->data as $row){
            $org = $row['total_org'];
            $rph = $row['total_rph'];
            $this->Cell($this->widths[0],5,$row['debtur'],1,0,'C');
            $this->Cell($this->widths[1],5,$row['debnam'],1,0,'L');
            $this->Cell($this->widths[2],5,FormatNumber($org,2),1,0,'R');
            $this->Cell($this->widths[3],5,FormatNumber($rph,2),1,0,'R');
            $this->Cell($this->widths[4],5,'0',1,1,'C');
            $totalORG += $org;
            $totalRPH += $rph;
          }
        }

        $this->Cell($this->widths[0]+$this->widths[1],7,'TOTAL CURRENCY: ',1,0);
        $this->Cell($this->widths[2],7,FormatNumber($totalORG,2),1,0,'R');
        $this->Cell($this->widths[3],7,FormatNumber($totalRPH,2),1,0,'R');
        $this->Cell($this->widths[4],7,'',1,0);
    }

}


?>
